<template>
  <el-dialog width="500px"  title="改价" :close-on-click-modal="false"  :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="订单编号" style="margin-bottom: 8px;width: 400px">
        {{dataForm.orderSn}}
      </el-form-item>
      <el-form-item label="原价" style="margin-bottom: 8px;width: 400px">
        {{dataForm.price}}元
      </el-form-item>
      <el-form-item label="增加" prop="additional" style="width: 400px">
        <el-input  @keyup.native="keyup()" v-model.trim="dataForm.additional" placeholder="增加"></el-input>
      </el-form-item>
      <el-form-item label="优惠" prop="discount" style="margin-bottom: 8px;width: 400px">
        <el-input  @keyup.native="keyup()" v-model.trim="dataForm.discount" placeholder="优惠"></el-input>
      </el-form-item>
      <el-form-item label="实际价格" style="margin-bottom: 8px;width: 400px">
        {{dataForm.actprice}}元
      </el-form-item>
      <el-form-item label="说明" style="margin-bottom: 8px;width: 400px">
        <el-input type="textarea" :rows="2" v-model="dataForm.extras" placeholder="说明"></el-input>
      </el-form-item>


    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        packages:[],
        visible: false,
        dataForm: {
          orderId: 0,
          price:0,
          actprice:0,
          additional:0,
          discount:0,
          extras:''
        },
        dataRule: {}
      }
    },
    methods: {
      keyup () {
        console.log(this.dataForm.actprice)
        let yuanjia=this.dataForm.price
        let zen=this.dataForm.additional
        let jian=this.dataForm.discount
        zen = zen=='' ? 0:zen
        jian= jian=='' ? 0 :jian
        this.dataForm.actprice=parseInt(this.dataForm.price)+parseInt(zen)-parseInt(jian)


      },
      init (id) {
        this.dataForm.orderId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.orderId) {
            this.$http({
              url: this.$http.adornUrl(`/order/v9order/getgaijia/${this.dataForm.orderId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderSn = data.v9Order.orderSn
                this.dataForm.additional = data.v9Order.additional
                this.dataForm.discount = data.v9Order.discount
                let obj = JSON.parse(data.price)
                this.dataForm.price = obj.fee
                this.dataForm.actprice = this.dataForm.price
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/order/v9order/fahuo`),
              method: 'post',
              data: this.$http.adornData({
                'orderId': this.dataForm.orderId || undefined,
                'additional': this.dataForm.additional,
                'discount': this.dataForm.discount,
                'extras': this.dataForm.extras,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
